package cn.edu.nuc.ssm.controller.film;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import cn.edu.nuc.ssm.controller.page.Pager;
import cn.edu.nuc.ssm.domain.Film;
import cn.edu.nuc.ssm.service.impl.ImgServiceImpl;
import cn.edu.nuc.ssm.service.interfaces.FilmService;

@Controller
@RequestMapping("film")
public class FilmController {

	@Autowired
	private FilmService filmServiceImpl;

	/*
	 * //初始化web应用传来的日期格式 也可以直接加注解解决。
	 * 
	 * @InitBinder public void initBinder(WebDataBinder binder) {
	 * SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
	 * dateFormat.setLenient(false); binder.registerCustomEditor(Date.class, new
	 * CustomDateEditor(dateFormat, false)); }
	 */
	@ModelAttribute("film")
	public Film getFilm() {
		Film film = new Film();
		return film;
	}

	// 通过get方式进入添加电影页面（get请求用来或得资源。）
	@RequestMapping(value = "add", method = RequestMethod.GET)
	public String getAdd() {
		return "film/add_film";
	}

	// 添加电影的映射。（post用来新建资源）
	@RequestMapping(value = "add", method = RequestMethod.POST)
	public String releaseFilm(Film film, @RequestParam("cover") MultipartFile cover,
			@RequestParam("picture") MultipartFile file, HttpServletRequest request, Model model) {
		// 得到上传图片的真实路径。
		String path = request.getServletContext().getRealPath("/assets/img");
		String filename = ImgServiceImpl.imgpath(path, file);
		String exname = ImgServiceImpl.exname(cover);
		String filmcover = ImgServiceImpl.imgpath(path, cover);
		if (filename != null) {
			film.setFilmPicture(filename);
		}
		if (filename != null) {
			if (exname.equals("yes")) {
				film.setFilmCover(filmcover);
			} else {
				model.addAttribute("pic", "不支持的文件类型，请重新上传！");
				return "film/add_film";
			}
		}
		int result = filmServiceImpl.releaseFilm(film);
		if (result == 1) {
			return "redirect:/film/list";
		}
		return "error";
	}

	/**
	 * 进入电影列表(查询)
	 * 
	 * @return
	 */
	@RequestMapping(value = "list", method = RequestMethod.GET)
	public String getFilm(@RequestParam(name = "filmname", defaultValue = "") String filmname,
			@RequestParam(name = "current", defaultValue = "1") int current, Model model) {
		Pager<Film> pager = filmServiceImpl.pageList(filmname, current);
		System.out.println(pager.getList().size());
		model.addAttribute("pager", pager);
		return "film/list";
	}

	/**
	 * 更新，（全部进行更新）
	 */
	// 获取电影信息
	@RequestMapping(value = "detail/{filmid}", method = RequestMethod.GET)
	public String updateFilm(@PathVariable("filmid") Integer filmid, Model model) {
		Film film = filmServiceImpl.selectByFilmid(filmid);
		System.out.println(film.getTicket());
		model.addAttribute("film", film);
		return "film/update";
	}

	// 更新
	@RequestMapping(value = "update/{filmid}", method = RequestMethod.PUT)
	public String updateFilm(@PathVariable("filmid") Integer filmid, Film film) {
		int result = filmServiceImpl.updateByPrimaryKey(film);
		if (result == 1) {
			return "redirect:/film/list";
		}
		return "redirect:error";
	}

	// 删除单个电影信息。
	@RequestMapping(value = "delete/{filmid}", method = RequestMethod.DELETE)
	public String deleteFun(@PathVariable("filmid") Integer filmid) {
		int result = filmServiceImpl.deleteByPrimaryKey(filmid);
		if (result == 1) {
			return "redirect:/film/list";
		}
		return "error";
	}
	
	//获得全部的电影
	@ResponseBody
	@RequestMapping(value="select",method=RequestMethod.GET)
	public List<Film> select(){
		List<Film> list = filmServiceImpl.select();
		if(list.size()>0){
			return list;
		}
		return null;
	}
	
	@RequestMapping(value = "select2", method = RequestMethod.GET)
	public String selectByHot(Model model) {
		List<List<Film>> list = filmServiceImpl.select2();
		if (list.size() > 0) {
			model.addAttribute("list", list);
			return "forward:/index.jsp";
		}
		return "error";
	}
	
}
